package com.gemme.gemme.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gemme.gemme.R;

import org.w3c.dom.Text;

/**
 * @Author: yangcheng
 * @CreateDate: 2020/7/1 11:14
 */
public class PriceView extends RelativeLayout {
    private Context context;
    int num = 1;
    private OnChangeLIstener onchangeListener;

    public PriceView(Context context) {
        this(context,null);
    }

    public PriceView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PriceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    TextView price_delete;
    TextView numText;
    TextView price_plus;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.price_view,this);
        price_delete = findViewById(R.id.price_delete);
        numText = findViewById(R.id.num);
        price_plus = findViewById(R.id.price_plus);

        price_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(num == 0){
                    Toast.makeText(context, "物品最低0个", Toast.LENGTH_SHORT).show();
                    return;
                }
                num --;
                setNum(num);
                if(onchangeListener != null)
                    onchangeListener.onChangeLIstener();
            }
        });

        numText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        price_plus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                num ++;
                setNum(num);
                if(onchangeListener != null)
                    onchangeListener.onChangeLIstener();
            }
        });
    }

    public void setNum(int num) {
        this.num = num;
        numText.setText(num + "");
    }

    public int getNum(){
        return Integer.parseInt(numText.getText() + "");
    }

    public void setOnchangeListener(OnChangeLIstener onchangeListener){

        this.onchangeListener = onchangeListener;
    }
}
